package com.waitingresult.designpattern.creation.singleton.type3;

public class SingletonDemo {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new SingletonThread());
        Thread thread2 = new Thread(new SingletonThread());
        Thread thread3 = new Thread(new SingletonThread());
        Thread thread4 = new Thread(new SingletonThread());
        Thread thread5 = new Thread(new SingletonThread());
        Thread thread6 = new Thread(new SingletonThread());
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();
    }
}

class SingletonThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Singleton.getInstance());
    }
}

class Singleton {
    private static Singleton instance;

    /**
     * 单例模式的第三种 实现方式
     * 来实现单例模式,  属于懒汉式模式
     * 优缺点:  优点, 属于懒加载
     * 缺点, 多线程会有问题
     */
    private Singleton() {
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}